package com.yzc.android.manager.location;

import android.os.Handler;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.yzc.android.KDApp;
import com.yzc.android.event.BDLocationResponseEvent;
import com.yzc.android.event.EventBusManager;
import com.yzc.android.utils.ThreadUtils;

/**
 * Created by yzc on 16/5/21.
 */
public enum BaiduLocationManager {
    INSTANCE;
    private static boolean isInitCalled = false;

    private int kLocationLocalTimeout = 30 * 1000; // 定位超时定为30s

    private Handler mTimeoutHandler = new Handler();

    private Runnable mTimeoutRunnable;

    public LocationClient mLocationClient = null;
    public static synchronized BaiduLocationManager getInstance() {
        if (!isInitCalled)
        {
            INSTANCE.init();
            isInitCalled = true;
        }
        return INSTANCE;
    }

    private void init() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span=1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(false);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(true);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要

        mLocationClient = new LocationClient(KDApp.getContext());
        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(final BDLocation bdLocation) {
                ThreadUtils.runOnMainThreadAsync(new Runnable() {
                    @Override
                    public void run() {
                        EventBusManager.getInstance().post(new BDLocationResponseEvent(bdLocation));
                    }
                });
            }
        });

        mTimeoutRunnable = new Runnable() {
            @Override
            public void run() {
                stopLocation();
                ThreadUtils.runOnMainThreadAsync(new Runnable() {
                    @Override
                    public void run() {
                        EventBusManager.getInstance().post(new BDLocationResponseEvent(null));
                    }
                });
                startLocation();
            }
        };
    }

    public void setLocalTimeout(int timeout) {
        kLocationLocalTimeout = timeout;
    }

    public void startLocation() {
        if (mLocationClient != null) {
            mLocationClient.start();
            mTimeoutHandler.removeCallbacks(mTimeoutRunnable);
            mTimeoutHandler.postDelayed(mTimeoutRunnable, kLocationLocalTimeout);
        }
    }

    public void stopLocation() {
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
    }

    public void requestLocation() {
        if (mLocationClient != null) {
            mLocationClient.requestLocation();
        }
    }
}
